import QRCode from '../../../../utils/qrcode.js'
import Serv from './gameTakePrizeCodeServ.js';
import Config from "../../../../utils/config2"

var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")
var app = getApp()
const W = wx.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;

// 300rpx 在6s上为 150px
const qrcode_w = 300 / rate;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: false,
    qrcode_w: qrcode_w,
    objectId:"",
    actionType:"",
    objectType:"GAME",
    prizeData:{},//数据信息
    qrcode:null,//二维码
    times:null,//定时
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      objectId: options.objectId,
      actionType: options.actionType
    });
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(this.data.times);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.times);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  async getData(){
    let that = this;
    let { qrcode,objectId, actionType, objectType } = that.data;
    let params = {
      objectId: objectId,
      actionType: actionType,
      objectType: objectType
    }
    let token = wx.getStorageSync(Config.authName)
    wx.request({
      url: `https://api-zj-sit.tq-service.com/api/agent/generateUrl`,
      method: "POST",
      data: params,
      header: {
        'token':token,
        'content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
      },
      success:function(res){
        let data = res.data.data;
        if (data) {
          if (qrcode == null){
            qrcode = new QRCode('canvas', {
              // usingIn: this,
              text: JSON.stringify(params),
              image: '',
              padding: 12,
              width: qrcode_w,
              height: qrcode_w,
              colorDark: "#000",
              colorLight: "white",
              correctLevel: QRCode.CorrectLevel.H,
              callback: (res) => {

              }
            });
            that.setData({
              loadingHidden:true
            });
          }
          that.setData({
            prizeData:data,
            qrcode: qrcode
          });
        }
      }
    });

    let times = setTimeout(function(){
      that.getData();
    },1000);

    that.setData({
      times:times
    });
    
  }
})